propsage.namespace("prestige.manage.users");

prestige.manage.users.update = (function($, ui){
	var prevOverrideId; 
	
	function initComponents()
	{
	
	}

	function initEventHandlers()
	{
		$('#User_referrer_user_id').change(onOverrideChange);
	}
	
	function onOverrideChange(event)
	{
		$('#tiers table').remove();
		
		var selectedOverride = $('#User_referrer_user_id').val();
		
		ui.doAjax('POST', baseUrl + '/admin/users/ajaxGetOverridings', {
			id: selectedOverride,
			user_id: userId
		}, onOverrideChangeSuccess, onOverrideChangeFailure);
	}
	
	function onOverrideChangeSuccess(response) {
		if (response.result == 'success') {
			
			var $table = $('<table></table>');
			$table.addClass('tierTable');
			
			var data = response.data;
			
			var $row = $('<tr></tr>');
			for(var key in data){
				key = Number(key) + 1;
				var string = '<td>Tier '+ key +'</td>';
				$row.append(string);
			}
			$row.appendTo($table);
			$row = $('<tr></tr>');
			for(var key in data){
				var string = '<td>'+data[key]+'</td>';
				$row.append(string);
			}
			$row.appendTo($table);
			$table.appendTo( '#tiers' );	
			prevOverrideId = $("#User_referrer_user_id option:selected").val();
		} else {
			$('#User_referrer_user_id').val(prevOverrideId);
		}
	}
	
	function onOverrideChangeFailure() {
		$('#User_referrer_user_id').val(prevOverrideId);
	}
	
	
	$(document).ready(function(){
		initComponents();
		initEventHandlers();
		onOverrideChange();
		
		prevOverrideId = $("#User_referrer_user_id option:selected").val();
	});
	
	return{};
	
}(jQuery, propsage.ui));